package cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.ui;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.HomeBean;
import cn.rongdao.jrkabao.ui.cashadvance.CashAdvanceActivity;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.growingio.BuriedUtil;
import cn.rongdao.jrkabao.widget.CenterAlignImageSpan;
import cn.rongdao.jrkabao.widget.MarqueeTextView;

/**
 * Created by WANG on 17/12/18
 */

public class CashHeadComponent extends RelativeLayout {


    public CashHeadComponent(Context context) {
        super(context);
    }

    public CashHeadComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CashHeadComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TextView cashTitle;
    private TextView cashAction;
    private TextView successMessage;
    private String institution_id;
    private MarqueeTextView marqueeTextView;
    private LinearLayout llMarquee;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        cashTitle = findViewById(R.id.home_head_title);
        cashAction = findViewById(R.id.home_head_action);
        successMessage = findViewById(R.id.home_head_successMessage);
        marqueeTextView = findViewById(R.id.lhh_tv_marquee);
        llMarquee = findViewById(R.id.lhh_ll_marquee);
        initListener();
    }

    private void initListener() {
        cashAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BuriedUtil.kamao_shouye_quxian_apply(getContext());
                CashAdvanceActivity.start(getContext(), institution_id);
            }
        });

    }

    public void setData(HomeBean.CashButtonBean cash_button, HomeBean.RollNoticeBean rollNoticeBean) {
        if (null != cash_button && CommonUtil.isStatusSuccess(cash_button.getIs_show())) {
            setVisibility(View.VISIBLE);
            institution_id = cash_button.getInstitution_id();

            String config_cn = cash_button.getConfig_cn();
            String cash_desc = cash_button.getCash_desc();
            String cash_message = cash_button.getCash_text();
            String cash_number = cash_button.getCash_number();
            if (TextUtils.isEmpty(config_cn)) {
                config_cn = "信用卡取现";
            }
            if (TextUtils.isEmpty(cash_message)) {
                cash_message = "今日通道剩余可取现额度";
            }
            if (TextUtils.isEmpty(cash_desc)) {
                cash_desc = "安全  秒到  保障";
            }
            cashTitle.setText(cash_desc);
            cashAction.setText(config_cn);
            setMessage(cash_message, cash_number);
        }
        if (null != rollNoticeBean && CommonUtil.isStatusSuccess(rollNoticeBean.getIs_show())) {
            llMarquee.setVisibility(View.VISIBLE);
            marqueeTextView.setText(FRCollection.appendAsString(rollNoticeBean.getMessage(), rollNoticeBean.getMessage(), rollNoticeBean.getMessage(), "              "));
            marqueeTextView.start();
        }
    }

    private void setMessage(String message, String number) {
        if (!FRString.isEmpty(number, message)) {
            if (number.length() == 1) {
                number = FRCollection.appendAsString("000", number, "");
            }
            if (number.length() == 2) {
                number = FRCollection.appendAsString("00", number, "");
            }
            if (number.length() == 3) {
                number = FRCollection.appendAsString("0", number, "");
            }
            String content = FRCollection.appendAsString(message, " ", number, " 万", "");
            Drawable drawable;
            SpannableString ss = new SpannableString(content);
            int start = message.length() + 1;
            for (int i = 0; i < number.length(); i++) {
                drawable = getDrawableByNumber(FRString.valueOf(number.charAt(i)));
                if (null != drawable) {
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    CenterAlignImageSpan span = new CenterAlignImageSpan(drawable);
                    ss.setSpan(span, start, (start + 1), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                }
                start++;
            }
            successMessage.setText(ss);
        }
    }

    private Drawable getDrawableByNumber(String number) {
        int resId;
        switch (number) {
            case "0":
                resId = R.drawable.icon_number_zero;
                break;
            case "1":
                resId = R.drawable.icon_number_one;
                break;
            case "2":
                resId = R.drawable.icon_number_two;
                break;
            case "3":
                resId = R.drawable.icon_number_three;
                break;
            case "4":
                resId = R.drawable.icon_number_four;
                break;
            case "5":
                resId = R.drawable.icon_number_five;
                break;
            case "6":
                resId = R.drawable.icon_number_six;
                break;
            case "7":
                resId = R.drawable.icon_number_seven;
                break;
            case "8":
                resId = R.drawable.icon_number_eight;
                break;
            case "9":
                resId = R.drawable.icon_number_nine;
                break;
            default:
                return null;
        }
        return ContextCompat.getDrawable(getContext(), resId);
    }
}
